package com.cash.app.ui.auth

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.constant.AppConstant.AUTH_TYPE_BANK
import com.cash.app.constant.AppConstant.AUTH_TYPE_BASIC
import com.cash.app.constant.AppConstant.AUTH_TYPE_PERSONAL
import com.cash.app.custom.AppActionBar
import com.cash.app.databinding.AcAuthBinding
import com.cash.app.manager.AccountInfoManager
import com.cash.app.manager.AuthMetaDataManager
import com.cash.app.utils.hide
import com.cash.app.utils.visible

class AuthActivity : BaseActivity() {

    private lateinit var binding: AcAuthBinding
    private var isBasic = false
    private var isPersonal = false
    private var isBank = false

    private val viewModel: AuthACViewModel by viewModels()

    override val statusBarColor: Int
        get() = R.color.transparent

    override val isFitsSystemWindows: Boolean
        get() = false

    override val isFitNavigationBarInsets: Boolean
        get() = true

    override fun setViewBind() {
        binding = AcAuthBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View {
        return binding.root
    }

    override fun getAppActionBar(): AppActionBar {
        return binding.authActionBar
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
        initObserver()
        loadMetaData()
    }

    private fun initObserver() {
        viewModel.metaData.observe(this) { data ->
            data?.let {
                AuthMetaDataManager.setMetaData(it)
            }
        }
        viewModel.accountDetail.observe(this) { data ->
            updateAuthStatus()
        }
    }

    private fun loadMetaData() {
        viewModel.getMetaData()
    }

    override fun onResume() {
        super.onResume()
        viewModel.getAccountDetail()
    }

    private fun updateAuthStatus() {
        isBasic = AccountInfoManager.isAuthCompleted(AUTH_TYPE_BASIC)
        isPersonal = AccountInfoManager.isAuthCompleted(AUTH_TYPE_PERSONAL)
        isBank = AccountInfoManager.isAuthCompleted(AUTH_TYPE_BANK)

        binding.apply {
            ivBasicCheck.visibility = if (isBasic) View.VISIBLE else View.GONE
            ivPersonalCheck.visibility = if (isPersonal) View.VISIBLE else View.GONE
            ivBankCheck.visibility = if (isBank) View.VISIBLE else View.GONE
        }
        if (AccountInfoManager.isAuthCompleted()) {
            binding.btnStart.hide()
        } else {
            binding.btnStart.visible()
        }
    }


    private fun initListener() {
        binding.apply {
            btnStart.setOnClickListener {
                startAuth()
            }
            cardBasic.setOnClickListener {
                if (!isBasic) {
                    startAuth()
                } else {
                    AuthBasicActivity.start(this@AuthActivity, isBasic)
                }
            }
            cardPersonal.setOnClickListener {
                if (!isPersonal) {
                    startAuth()
                } else {
                    AuthPersonalActivity.start(this@AuthActivity, isPersonal)
                }
            }

            cardBank.setOnClickListener {
                if (!isBank) {
                    startAuth()
                } else {
                    AuthBankActivity.start(this@AuthActivity, isBank)
                }
            }
        }
    }

    private fun startAuth() {
        if (!isBasic) {
            AuthBasicActivity.start(this@AuthActivity, isBasic)
            return
        }
        if (!isPersonal) {
            AuthPersonalActivity.start(this@AuthActivity, isPersonal)
            return
        }
        if (!isBank) {
            AuthBankActivity.start(this@AuthActivity, isBank)
            return
        }
    }
}